<?php
/**存取钱*/
class ChangeBankMoney extends APIBase{
	
	public $tag = "ChangeBankMoney";
	public $isLogin = true;
	
	public function before() 
	{
		$this->initCacheRedis();
		$this->initMysql();
		return true;
	}
	
	public function logic() 
	{
		
		if ( !isset($this->param['change_flag']) )
		{
			$this->returnError(401, "未设置存取标志");
		}
		else 
		{
			$flag = $this->param['change_flag'];
		}
				
		if ( !isset($this->param['exchange_money']) ) 
		{
			$this->returnError(402,"未设置存钱金额");
		} 
		else 
		{
			$exchangeMoney = $this->param['exchange_money'];
			
			if ($exchangeMoney <= 0)
			{
				$this->returnError(403, "存取金额必须大于0");
			}
		}
		
		if ( ($flag == 1) && (!isset($this->param['bank_password'])) )
		{
			//取钱，但是没密码
			$this->returnError(404, "未输入银行密码");
		}
		
		
		$ret = $this->data_redis->hMGet("hu:{$this->uid}", array('money','bank', 'bank_password', 'bank_salt','online_vid'));
		
		
		$ret['money'] = (int) $ret['money'];
		$ret['bank'] = (int) $ret['bank'];
        //判断游戏中不能取钱
		if((int)$ret['online_vid']>0){
			$this->returnError(409, "正在游戏中,不能存取款");
		}
	   
		if ($flag == 0)		//存钱
		{
			if ($ret['money'] >= $exchangeMoney)
			{
				$ret['money'] -= $exchangeMoney;
				$ret['bank'] += $exchangeMoney;
				//存钱记录
				$binfo =array();
			    $binfo['RoomID'] = 0;
			    $binfo['UserID']  = $this->uid;
				$binfo['MoneyInBank']  = $ret['bank'];
				$binfo['MoneyInRoom'] = $ret['money'];
				$binfo['OutMoney']    = 0;
				$binfo['InMoney']  = $exchangeMoney;
				$binfo['ChangeType'] = 1;
				$binfo['CollectTime']  = time();
				$result = $this->mysql->insert("changemoneyrecord",$binfo);
				
				
				//转账记录
				$m_rec['type'] = 6;
				/*$m_rec['vid'] = 0;
				$m_rec['tid']  = 0;
				$m_rec['seatid']  = 0;*/
				$m_rec['uid']  =  $this->uid;
				$m_rec['money'] =$ret['money'];
				$m_rec['change_money']  =  -$exchangeMoney;
				$this->addChangeMongeyLog($m_rec);  
				
			}
			else 
			{
				$this->returnError(405, "存款金额大于本身拥有的金币数量");
			}
		}
		else if ($flag == 1)		//取钱
		{
			$old_password = md5($this->param['bank_password'].$ret['bank_salt']);
			
			if ($old_password != $ret['bank_password'])
			{
				//密码不对
				$this->returnError(406, "取款密码错误");
			}
			else 
			{
				if ($ret['bank'] >= $exchangeMoney)
				{
					$ret['money'] += $exchangeMoney;
					$ret['bank'] -= $exchangeMoney;
					//取钱记录
					
					$binfo =array();
			        $binfo['RoomID'] = 0;
			        $binfo['UserID']  = $this->uid;
					$binfo['MoneyInBank']  = $ret['bank'];
					$binfo['MoneyInRoom'] = $ret['money'];
					$binfo['OutMoney']    = $exchangeMoney;
					$binfo['InMoney']  = 0;
					$binfo['ChangeType'] = 1;
					$binfo['CollectTime']  = time();
					$result =$this->mysql->insert("changemoneyrecord",$binfo); 
					
					$m_rec['type'] = 6;
					/*$m_rec['vid'] = 0;
					$m_rec['tid']  = 0;
					$m_rec['seatid']  = 0;*/
					$m_rec['uid']  =  $this->uid;
					$m_rec['money'] =$ret['money'];
					$m_rec['change_money']  =  $exchangeMoney;
					$this->addChangeMongeyLog($m_rec); 
					
				}
				else
				{
					$this->returnError(407, "取款金额大于保险箱拥有的金额");
				}
			}
		}
		else 
		{
			$this->returnError(408, "存取标志错误");
		}
		
		
		$this->data_redis->hMset("hu:{$this->uid}", $ret);
		
		$result = $this->data_redis->hMget("hu:{$this->uid}", array('money', 'bank'));
		$result['money'] = (int)$result['money'];
		$result['bank'] = (int)$result['bank'];
		
		$this->returnData($result,0, "存取款成功");
		
		if ($flag ==0 && $exchangeMoney >= 15000)
		{
			$this->hander_coin_task_complete(4);//第4个任务是否完成,首次使用保险箱完成一笔15000以上的充值任务是否完成
		}

	}
	
	public function hander_coin_task_complete($task_id) {
		//从奖券任务记录字段中获取奖券任务完成度
		$task_complete_record_str = $this->data_redis->hget("hu:{$this->uid}","coin_task_complete_record");
		$task_pieces = explode("|", $task_complete_record_str);
		$task_value = $this->cache_redis->hget("hcointask:{$task_id}","task_value");
		//如果任务完成度 < 最大任务完成值,则任务完成度加1
		if ( (int)$task_pieces[$task_id - 1] < (int)$task_value)
		{
			$task_pieces[$task_id - 1] += 1;
		}
		else
		{
			return;
		}
			
		//重新组装，任务奖励记录字符串
		$size = count($task_pieces);
		$str="";
		for ($i=0; $i<$size; $i++)
		{
		$str .= $task_pieces[$i];
		if ($i < $size-1)
		{
		$str .= "|";
		}
		}
		//重新设置任务奖励记录字符串
		$this->data_redis->hset("hu:{$this->uid}","coin_task_complete_record",$str);
			return;
	}
		
	public function after() 
	{
		$this->deinitMysql();
		$this->deinitCacheRedis();
		$this->deinitDataRedis();
	}
}